import axios from "axios";
import { ElMessage } from "element-plus";
import { store } from "@/utils/global";
export const http = axios.create({
    baseURL: import.meta.env.VITE_BASE_API,
    withCredentials: true,
    timeout: 60000,
    headers: {
        "X-Requested-With": "XMLHttpRequest",
    },
});
/**
 * 请求拦截器，
 */
http.interceptors.request.use(
    (config) => {
        // 请求携带token
        const token = store.token;
        if (token != "" && token != null) {
            config.headers["Authorization"] = token;
        }
        return config;
    },
    (error) => {
        return Promise.reject(error);
    }
);

/**
 * 返回拦截器，
 */
http.interceptors.response.use(
    (response) => {
        const res = response.data;
        if (!res.success) {
            if (res.code == "A00004") {
                // ElMessage.error("登录已过期，请重新登录!");
                ElMessage.error("token无效!");
            } else {
                ElMessage.error(res.msg);
            }
            return Promise.reject(res);
        }
        return res;
    },
    (error) => {
        if (String(error).includes("timeout")) {
            ElMessage.warning("接口请求超时!");
            return;
        }
        if (String(error).indexOf("Network") != -1) {
            ElMessage.warning("当前网络状态欠佳!");
            return;
        }
        return Promise.reject(error.response);
    }
);
